package com.controller;

import com.Tool.HCNetSDK;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;
import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping(value = "/park/interface")
public class testController{
    static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
    //保存日志功能每5分钟推送一次信息
    @GetMapping(value = "/opens")
    public int openDoor() {
        int iErr = 0;
        HCNetSDK.NET_DVR_CARD_CFG_COND m_struCardInputParam = new HCNetSDK.NET_DVR_CARD_CFG_COND();
        m_struCardInputParam.dwSize = m_struCardInputParam.size();
        m_struCardInputParam.dwCardNum = 0xffffffff; //查找全部
        m_struCardInputParam.byCheckCardNo = 1;

        Pointer lpInBuffer = m_struCardInputParam.getPointer();
        fRemoteCfgCallBackCardGet fremoteCfgCallBackCardGet=new fRemoteCfgCallBackCardGet();
        m_struCardInputParam.write();

        HCNetSDK.MY_USER_DATA userData = new HCNetSDK.MY_USER_DATA();
        userData.dwSize = userData.size();
        userData.byteData = "1234567".getBytes();
        Pointer pUserData = userData.getPointer();
        userData.write();

        NativeLong lHandle = hCNetSDK.NET_DVR_StartRemoteConfig(lianjie.lianjie(), HCNetSDK.NET_DVR_GET_CARD_CFG_V50, lpInBuffer, m_struCardInputParam.size(), fremoteCfgCallBackCardGet, pUserData);
        if (lHandle.intValue() < 0)
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            JOptionPane.showMessageDialog(null, "建立长连接失败，错误号：" + iErr);
            return 2;
        }
       // JOptionPane.showMessageDialog(null, "建立获取卡参数长连接成功!");
        System.out.println("建立获取卡参数长连接成功!");
     /*	//查找指定卡号
      HCNetSDK.NET_DVR_CARD_CFG_SEND_DATA m_struCardSendInputParam = new HCNetSDK.NET_DVR_CARD_CFG_SEND_DATA();
	m_struCardSendInputParam.read();
	m_struCardSendInputParam.dwSize = m_struCardSendInputParam.size();
	m_struCardSendInputParam.byCardNo = "111010".getBytes();
	m_struCardSendInputParam.byRes = "0".getBytes();

        Pointer pSendBuf = m_struCardSendInputParam.getPointer();
        m_struCardSendInputParam.write();

        if(!hCNetSDK.NET_DVR_SendRemoteConfig(lHandle, 0x3, pSendBuf, m_struCardSendInputParam.size()))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            JOptionPane.showMessageDialog(null, "ENUM_ACS_SEND_DATA失败，错误号：" + iErr);
            return;
        }*/

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if(!hCNetSDK.NET_DVR_StopRemoteConfig(lHandle))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            JOptionPane.showMessageDialog(null, "断开长连接失败，错误号：" + iErr);
            return 2;
        }
       // JOptionPane.showMessageDialog(null, "断开长连接成功!");
        System.out.println("断开长连接成功!");
        return 1;
    }
    @GetMapping(value = "/zsq")
    public void SetCardInfo() throws UnsupportedEncodingException

    {

        int iErr = 0;

        //设置卡参数
        HCNetSDK.NET_DVR_CARD_CFG_COND m_struCardInputParamSet = new HCNetSDK.NET_DVR_CARD_CFG_COND();
        m_struCardInputParamSet.read();
        m_struCardInputParamSet.dwSize = m_struCardInputParamSet.size();
        m_struCardInputParamSet.dwCardNum = 1;
        m_struCardInputParamSet.byCheckCardNo = 1;

        Pointer lpInBuffer = m_struCardInputParamSet.getPointer();
        m_struCardInputParamSet.write();

        Pointer pUserData = null;
        FRemoteCfgCallBackCardSet fRemoteCfgCallBackCardSet = new FRemoteCfgCallBackCardSet();

        NativeLong lHandle = hCNetSDK.NET_DVR_StartRemoteConfig( lianjie.lianjie(), HCNetSDK.NET_DVR_SET_CARD_CFG_V50, lpInBuffer, m_struCardInputParamSet.size(), fRemoteCfgCallBackCardSet, pUserData);
        if (lHandle.intValue() < 0)
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            JOptionPane.showMessageDialog(null, "建立长连接失败，错误号：" + iErr);
            return;
        }
        //JOptionPane.showMessageDialog(null, "建立设置卡参数长连接成功!");
        System.out.println("建立设置卡参数长连接成功!");

        HCNetSDK.NET_DVR_CARD_CFG_V50 struCardInfo = new HCNetSDK.NET_DVR_CARD_CFG_V50(); //卡参数
        struCardInfo.read();
        struCardInfo.dwSize = struCardInfo.size();
        struCardInfo.dwModifyParamType = 0x00000001 + 0x00000002 + 0x00000004 + 0x00000008 +
                0x00000010 + 0x00000020 + 0x00000080 + 0x00000100 + 0x00000200 + 0x00000400 + 0x00000800;
        /***
         * #define CARD_PARAM_CARD_VALID       0x00000001  //卡是否有效参数
         * #define CARD_PARAM_VALID            0x00000002  //有效期参数
         * #define CARD_PARAM_CARD_TYPE        0x00000004  //卡类型参数
         * #define CARD_PARAM_DOOR_RIGHT       0x00000008  //门权限参数
         * #define CARD_PARAM_LEADER_CARD      0x00000010  //首卡参数
         * #define CARD_PARAM_SWIPE_NUM        0x00000020  //最大刷卡次数参数
         * #define CARD_PARAM_GROUP            0x00000040  //所属群组参数
         * #define CARD_PARAM_PASSWORD         0x00000080  //卡密码参数
         * #define CARD_PARAM_RIGHT_PLAN       0x00000100  //卡权限计划参数
         * #define CARD_PARAM_SWIPED_NUM       0x00000200  //已刷卡次数
         * #define CARD_PARAM_EMPLOYEE_NO      0x00000400  //工号
         * #define CARD_PARAM_NAME             0x00000800  //姓名
         */

        String strCardNo = "2048844175";
        for (int i = 0; i < HCNetSDK.ACS_CARD_NO_LEN; i++)
        {
            struCardInfo.byCardNo[i] = 0;
        }
        for (int i = 0; i <  strCardNo.length(); i++)
        {
            struCardInfo.byCardNo[i] = strCardNo.getBytes()[i];
        }


        struCardInfo.byCardValid = 1;
        struCardInfo.byCardType =1;
        struCardInfo.byLeaderCard = 0;
        struCardInfo.byDoorRight[0]  = 1; //门1有权限
        struCardInfo.wCardRightPlan[0].wRightPlan[0] = 1; //门1关联卡参数计划模板1

        //卡有效期
        struCardInfo.struValid.byEnable = 1;
        struCardInfo.struValid.struBeginTime.wYear = 2017;
        struCardInfo.struValid.struBeginTime.byMonth = 12;
        struCardInfo.struValid.struBeginTime.byDay = 1;
        struCardInfo.struValid.struBeginTime.byHour = 0;
        struCardInfo.struValid.struBeginTime.byMinute = 0;
        struCardInfo.struValid.struBeginTime.bySecond = 0;
        struCardInfo.struValid.struEndTime.wYear = 2018;
        struCardInfo.struValid.struEndTime.byMonth = 12;
        struCardInfo.struValid.struEndTime.byDay = 1;
        struCardInfo.struValid.struEndTime.byHour = 0;
        struCardInfo.struValid.struEndTime.byMinute = 0;
        struCardInfo.struValid.struEndTime.bySecond = 0;

        struCardInfo.dwMaxSwipeTime = 0; //无次数限制
       // struCardInfo.dwSwipeTime = 0;
        struCardInfo.byCardPassword = "12346".getBytes();
        struCardInfo.dwEmployeeNo = 22;

        byte[] strCardName = "测试".getBytes("GBK");
        for (int i = 0; i < HCNetSDK.NAME_LEN; i++)
        {
            struCardInfo.byName[i] = 0;
        }
        for (int i = 0; i <  strCardName.length; i++)
        {
            struCardInfo.byName[i] = strCardName[i];
        }

        struCardInfo.write();
        Pointer pSendBufSet = struCardInfo.getPointer();

        if(!hCNetSDK.NET_DVR_SendRemoteConfig(lHandle, 0x3, pSendBufSet, struCardInfo.size()))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            JOptionPane.showMessageDialog(null, "ENUM_ACS_SEND_DATA失败，错误号：" + iErr);
            return;
        }

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if(!hCNetSDK.NET_DVR_StopRemoteConfig(lHandle))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            JOptionPane.showMessageDialog(null, "断开长连接失败，错误号：" + iErr);
            return;
        }
       // JOptionPane.showMessageDialog(null, "断开长连接成功!");
        System.out.println("断开长连接成功!");
    }


}
